// pages/chat/chat.js
const app = getApp();
var funcs = require('../../static/js/funcs.js');
Page({

  /**A
   * 页面的初始数据
   */
  data: {
    tishi: "请选择群组和日期",
    select_date: null,
    topic_desc:"",
    topic_type: "",
    descInputholder: "请输入题目内容",
    typeInputholder: "请输入题目类型"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let openid = app.globalData.openid
    funcs.getGroupAdmin(openid).then(res=>{
      if(res.data.succeed){
        let grouplist = res.data.data.groups
        console.log(grouplist)
        that.setData({
          grouplist: grouplist
        })     
        if(grouplist.length>0){
          that.setData({
            selectedgroup: grouplist[0].groupId,
            select_date:grouplist[0].status
          })
          this.fresh()
        }
      }
    })
  },
  // 改变日期
  select_date_func: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      select_date:e.detail.value
    })
    if(this.data.selectedgroup!=null&&this.data.select_date!=null){
      this.fresh()
    }
  },
  select_group_button(e){
    let groupid = e.currentTarget.dataset.groupid;
    this.setData({
      selectedOpenDate:e.currentTarget.dataset.displaystatus
    })
    console.log("选中的组群为：" + groupid)
    let that = this
    this.setData({
      selectedgroup: groupid
    })
    if(this.data.selectedgroup!=null&&this.data.select_date!=null){
      this.fresh()
    }else if(this.data.selectedgroup!=null&&this.data.select_date==null){
      let openid = app.globalData.openid
      funcs.getAssGroup(openid,this.data.selectedgroup,"").then(res=>{
        if(res.data.succeed){
          if(res.data.data.code==true){
            that.setData({
              openFlag:true,
              openedDate: this.data.select_date
            })
          }else{
            let date = res.data.data.date;
            that.setData({
              openFlag:false,
              openedDate: date
            })
          }
        }else{
          that.setData({
            openFlag:false,
            openedDate:null
          })
        }
      })
    }
  },

  fresh(){
    let that = this
    console.log("获取画像详情请求")
    let openid = app.globalData.openid
    funcs.getTopiclistAdmin(openid,this.data.selectedgroup,this.data.select_date).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data.topics)
        that.setData({
          topics:res.data.data.topics
        })
      }else{

      }
    })
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  addTopic(e){
    this.setData({
      display_flag: true,
      doType: 1,  // 1是添加 2是修改 
      descInputholder: "请输入题目内容",
      typeInputholder: "请输入题目类型"
    })
  },
  todoProcess(e){
    let openid = app.globalData.openid
    let that = this
    if(this.data.doType==1){//添加
      if(this.data.topic_desc.length==0||this.data.topic_type.length==0){
        wx.showToast({
          title: '请补充题目内容',
          icon: "error",
          duration: 2000
        })
        return
      }
      funcs.addTopic(openid,this.data.selectedgroup,this.data.select_date,
        this.data.topic_desc,this.data.topic_type).then(res=>{
          if(res.data.succeed){
            console.log("添加成功")
            that.fresh()
            that.setData({
              display_flag: false
            })
          }
      })
    }else if(this.data.doType==2){//修改
      funcs.modifyTopic(openid,this.data.selected_topicid,this.data.topic_desc,this.data.topic_type).then(res=>{
        if(res.data.succeed){
          console.log("修改成功")
          that.fresh()
          that.setData({
            display_flag: false
          })
        }
      })
    }
  },
  modify(e){
    let that = this
    let topicid = e.currentTarget.dataset.topicid;
    let openid = app.globalData.openid

    funcs.getTopic(openid,topicid).then(res=>{
      if(res.data.succeed){
        that.setData({
          display_flag: true,
          doType: 2, // 1是添加 2是修改 
          selected_topicid: topicid,
          descInputholder :res.data.data.topic.topicDesc,
          typeInputholder :res.data.data.topic.topicType,
          topic_type:res.data.data.topic.topicType,
          topic_desc:res.data.data.topic.topicDesc
        })
      }
    })
  },

  closeMask:function(){
    this.setData({
      display_flag: false,
      doType: 0
    })
  },
  typeInput:function(e){
    let onInput = e.detail.value
    console.log(e.detail.value)
    this.setData({
      topic_type:onInput
    })
  },
  descInput:function(e){
    let onInput = e.detail.value
    console.log(e.detail.value)
    this.setData({
      topic_desc:onInput
    })
  },
  delete(e){
    let topicid = e.currentTarget.dataset.topicid;
    let openid = app.globalData.openid
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定刪除吗？',
      success(res) {
        if (res.confirm) {
          funcs.deleteTopic(openid,topicid).then(res=>{
            if(res.data.succeed){
              console.log("删除成功")
              that.fresh()
            }
          })
        }
      }
    })
  }
})